/**********************************************************************/
/*  SECTION 1: Data imputation	
    Notes: 
	Author: Fumiya Uchikoshi

		*/
/**********************************************************************/
use "Data/base.dta",clear

*Edit values before imputation
replace onsenchi_std=onsenchi_std/1000
replace income = income/(cpi/100)
recode exdm_m 2=1 3=1

**********************************
****       Imputation        *****
**********************************

keep after exdm_m farming construction logpop income unemployment lfp onsenchi_std turnout homerate crime under15 married_male health ldpvote totcwins logpot onsenchi sumexlagdm tendiv nptypex changebde year yearx cpi visitor sumplant
mi set mlong
mi register imputed farming construction logpop income unemployment lfp onsenchi_std turnout homerate crime under15 married_male health ldpvote totcwins logpot onsenchi sumexlagdm tendiv nptypex changebde year cpi visitor sumplant
mi impute chained (regress) farming construction logpop income unemployment lfp onsenchi_std turnout homerate crime under15 married_male health ldpvote totcwins = logpot onsenchi sumexlagdm tendiv nptypex changebde year cpi visitor sumplant, ///
add(10) rseed (53421)

mi xtset after yearx
tabulate nptypex,gen(nptypex)
recode tendiv 1=1 2=2 9=9 else=0, gen(fourdiv)
tabulate fourdiv,gen(fourdiv)
tabulate yearx,gen(yearx)

bysort _mi_m after: egen logpop_b=mean(logpop)
bysort _mi_m after: egen farming_b=mean(farming)
bysort _mi_m after: egen unemployment_b=mean(unemployment)
drop nptypex3
gen nptypex3=0
replace nptypex3=1 if nptypex1==1 |  nptypex2==1
bysort _mi_m after: egen nptypex3_b=mean(nptypex3)
bysort _mi_m after: egen ldpvote_b=mean(ldpvote)
bysort _mi_m after: egen turnout_b=mean(turnout)
bysort _mi_m after: egen changebde_b=mean(changebde)
bysort _mi_m after: egen onsenchi_b=mean(onsenchi)
bysort _mi_m after: egen sumexlagdm_b=mean(sumexlagdm)
bysort _mi_m after: egen income_b=mean(income)
bysort _mi_m after: egen crime_b=mean(crime)
bysort _mi_m after: egen health_b=mean(health)

bysort _mi_m after: egen marmale_b=mean(married_male)
bysort _mi_m after: egen construction_b=mean(construction)
bysort _mi_m after: egen under15_b=mean(under15)
bysort _mi_m after: egen totcwins_b=mean(totcwins)
bysort _mi_m after: egen homerate_b=mean(homerate)
bysort _mi_m after: egen lfp_b=mean(lfp)
bysort _mi_m after: egen onsenchi_std_b=mean(onsenchi_std)

gen logpop_w=logpop-logpop_b
gen income_w=income-income_b
gen crime_w=crime-crime_b
gen health_w=health-health_b
gen farming_w=farming-farming_b
gen unemployment_w=unemployment-unemployment_b
gen nptypex3_w=nptypex3-nptypex3_b
gen ldpvote_w=ldpvote-ldpvote_b
gen turnout_w=turnout-turnout_b
gen changebde_w=changebde-changebde_b
gen onsenchi_w=onsenchi-onsenchi_b
gen sumexlagdm_w=sumexlagdm-sumexlagdm_b

gen marmale_w=married_male-marmale_b
gen construction_w=construction-construction_b
gen under15_w=under15-under15_b
gen onsenchi_std_w=onsenchi_std-onsenchi_std_b
gen totcwins_w=totcwins-totcwins_b
gen lfp_w=lfp-lfp_b
gen homerate_w=homerate-homerate_b
